<?php

namespace App\Console\Commands;

use App\Models\Article\ArticleOrder;
use App\Models\Constant\Constant;
use App\Models\User\MemberOrder;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Text;
use Illuminate\Console\Command;

class NoticeMoney extends Command
{

    protected $signature = 'notice:money';


    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle() {

        //今日收益
        $today = date('Y-m-d');
        $todayMemberFee = MemberOrder::whereDate('created_at',$today)->where('is_finished',1)->where('is_auto',1)->whereNotIn('user_id',[2,3,4,5])->sum('fee');//今日收益
        $todayArticleFee = ArticleOrder::whereDate('created_at',$today)->where('is_finished',1)->whereNotIn('user_id',[2,3,4,5])->sum('fee');//今日收益
        $todayFee = round($todayMemberFee+$todayArticleFee,1);

        //总收益
        $memberFee = MemberOrder::whereNotIn('user_id',[2,3,4,5])
            ->where('created_at','>=',Constant::SUB_TIME)
            ->where('is_finished',1)
            ->where('is_auto',1)
            ->sum('fee');//总收益

        $articleFee = ArticleOrder::whereNotIn('user_id',[2,3,4,5])
            ->where('created_at','>=',Constant::SUB_TIME)
            ->where('is_finished',1)
            ->sum('fee');

        $countFee = round($memberFee+$articleFee,1);
        if($countFee<=0)return 0;

        $config = config('wechat.default');
        $app = Factory::officialAccount($config);
        $time = date('Y-m-d H:i:s');
        $openId = 'oU4Mm6Onl4pbgnvPjkwLHBDdajvY';
        $templateId = 'ipXgJ89lndIRhq5enMyrzYenZSJ4ZeNAAg4Sp65skEI';
        $app->template_message->send([
            'touser' => $openId,
            'template_id' => $templateId,
            'url' => '',
            'data' => [
                'keyword1' => '',
                'keyword2' => '',//收款内容
                'keyword3' => '',//收款时间
                'keyword4' =>"截止到$time ，今日收益：$todayFee",//交易状态
                'keyword5' => "总收益：$countFee" //交易说明
            ]
        ]);


        return 0;
    }
}
